package com.wst.wst_server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.wst_server.entity.Money;
import com.wst.wst_server.entity.Org;
import com.wst.wst_server.service.IMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * �������� 前端控制器
 * </p>
 *
 * @author zyk
 * @since 2023-03-24
 */
@RestController
@RequestMapping("/money")
public class MoneyController {
    @Autowired
    IMoneyService service;

    @RequestMapping("/getbyid")
    public ResponseResult getbyid(Integer id){
        return new ResponseResult(200,"ok",service.getById(id));
    }
    @RequestMapping("/getbyidupdate")
    public ResponseResult getbyidupdate(Money money){
        service.updateById(money);
        return new ResponseResult(200,"ok",null);
    }
    @RequestMapping("/add")
    public ResponseResult addby(Money money){
        boolean b = service.save(money);
        return new ResponseResult(b?200:500,b?"添加成功":"添加失败",null);
    }
    @RequestMapping("/page")
    public ResponseResult<IPage> bypage(Integer page, Integer count,Integer orgId){
        IPage<Money> p = new Page<>(page,count);
        return new ResponseResult<IPage>(200,"ok",service.page(p,
                new QueryWrapper<Money>().eq("orgid",orgId).orderByDesc("yearly")));
    }

    @RequestMapping("/getbypage")
    public ResponseResult getbypage(Integer page, Integer count){
        IPage<Money> p = new Page<>(page,count);
        return new ResponseResult(200,"ok", service.page(p,new QueryWrapper<Money>().orderByDesc("yearly")));
    }

    @RequestMapping("/getbycondition")
    public ResponseResult<IPage<Money>> getbycondition(@RequestParam Map<String,Object> map){
        System.out.println(map);
        IPage<Money> ipage = service.getbycondition(map);
        return new ResponseResult<IPage<Money>>(200,"ok",ipage);
    }

    @RequestMapping("/getbyyearlyandorgid")
    public ResponseResult<List<Money>> getbyyearlyandorgid(@RequestParam Map<String,Object> map){
        System.out.println(map);
//        service.getbyyearlyandorgid(map);
        return new ResponseResult<List<Money>>(200,"ok",service.getyearlyandorgid(map));
    }

    @RequestMapping("deletebyid")
    public ResponseResult delete(Integer id){
        return new ResponseResult(200,"ok",service.removeById(id));
    }


}
